home *** CD-ROM | disk | FTP | other *** search
- * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
- * The C++ Answer Book */
- * Tony Hansen */
- * All rights reserved. */
- include <stream.h>
- include <string.h>
-
- lass string {
- struct srep {
- char *s;
- int n;
- };
- srep *p;
-
- ublic:
- string(char *); // string x = "abc"
- string(); // string x;
- string(string&); // string x = string ...
- string& operator=(char *);
- string& operator=(string &);
- ~string();
- char& operator[](int i);
- ifdef _6_3 // DELETE
- include <6_3st1.h> // DELETE
- endif /* _6_3 */ // DELETE
-
- friend ostream& operator<<(ostream&, string&);
- friend istream& operator>>(istream&, string&);
-
- friend int operator==(string &x, char *s)
- { return strcmp(x.p->s, s) == 0; }
-
- friend int operator==(string &x, string &y)
- { return strcmp(x.p->s, y.p->s) == 0; }
-
- friend int operator!=(string &x, char *s)
- { return strcmp(x.p->s, s) != 0; }
-
- friend int operator!=(string &x, string &y)
- { return strcmp(x.p->s, y.p->s) != 0; }
-
- ifdef _6_1 // DELETE
- riend class string_iterator; // DELETE
- ifdef A // DELETE
- include <6_1a1a.c> // DELETE
- endif // DELETE
- ifdef B // DELETE
- include <6_1a2a.c> // DELETE
- endif // DELETE
- // DELETE
- friend string operator+(string& s1, string& s2); // DELETE
- string& operator+=(string& s2); // DELETE
- endif /* _6_1 */ // DELETE
-
- ifdef _6_2 // DELETE
- string operator()(int i, int len = -1); // DELETE
- endif /* _6_2 */ // DELETE
-
- ;
-
- tring::string()
-
- p = new srep;
- p->s = 0;
- p->n = 1;
-
-
- tring::string(char *s)
-
- p = new srep;
- p->s = new char[ strlen(s) + 1 ];
- strcpy(p->s, s);
- p->n = 1;
-
-
- tring::string(string& x)
-
- x.p->n++;
- p = x.p;
-
-
- tring::~string()
-
- if (--p->n == 0) {
- delete p->s;
- delete p;
- }
-
-
- tring& string::operator=(char *s)
-
- if (p->n > 1) { // disconnect self
- p->n--;
- p = new srep;
- } else if (p->n == 1)
- delete p->s;
-
- p->s = new char[ strlen(s) +1 ];
- strcpy(p->s, s);
- p->n = 1;
- return *this;
-
-
- tring& string::operator=(string& x)
-
- x.p->n++;
- if (--p->n == 0) {
- delete p->s;
- delete p;
- }
- p = x.p;
- return *this;
-
-
- stream& operator<<(ostream& s, string& x)
-
- return s << x.p->s << " [" << x.p->n << "]\n";
-
-
- stream& operator>>(istream& s, string& x)
-
- char buf[256];
- s >> buf;
- x = buf;
- cout << "echo: " << x << "\n";
- return s;
-
-
- include <error.h>
-
- har& string::operator[] (int i)
-
- if (i < 0 || strlen(p->s) < i)
- error("index out of range");
- return p->s[i];
-
-